package com.android.openapp.ui;

import java.util.ArrayList;

import com.android.openapp.data.Place;
import com.android.openapp.services.Controller.ControllerBinder;
import com.android.openapp.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;

/**
 * Fragment che gestisce la visualizzazione di una lista di luoghi
 * @author Simone Panza
 *
 */
public class PlacesListFragment extends ListFragment implements FragmentInteract{
	//ArrayAdapter<String> la;
	LocationAdapter locadapter;
	SimpleCursorAdapter mAdapter;
	
	MainActivity mActivity;
	Button mButRefresh;
	Button mButSearch;
	Button mButBookmark;
	/**
	 * empty constructor, as requested from Android documentation
	 */
	public PlacesListFragment(){
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.places_list, container, false);
		
		mButRefresh = (Button) v.findViewById(R.id.button_list_refresh);
		mButSearch = (Button) v.findViewById(R.id.button_list_search);
		mButBookmark=(Button) v.findViewById(R.id.button_list_bookmarks);
		//tasto refresh
		mButRefresh.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.v(MainActivity.TAG,"tasto refresh");
				bookmarkFlag = FLAG_LIST_REFRESH;
				if(mBinder!=null)
					updateNearPlaces();
				//updateListPlaces();
			}
		});
		
		//tasto search
		mButSearch.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("search","onClick()");
				bookmarkFlag = FLAG_LIST_REFRESH;
				Boolean l=mActivity.onSearchRequested();
				Log.d("search",Boolean.toString(l));
					
			}
		});
		mButBookmark.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("bookmarks","onClick()");
				bookmarkFlag = FLAG_LIST_BOOKMARKS;
				onBookmarksListUpdated();
			}
		});
		
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.v(MainActivity.TAG, "PlacesListFragment.onActivityCreated()");
		
		mActivity = (MainActivity) getActivity();
		mActivity.setCurrentFragment(this);
		//prendo il binder ogni volta dalla MainActivity
		mBinder = mActivity.mBinder;
		
		getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, 
					int position, long id) {
				//Place p = (Place) parent.getAdapter().getItem(position);
				ShowInfoDialog mDialog = new ShowInfoDialog(getActivity(), R.layout.place_info, 
						R.string.button_dialog_text_list, position);
				mDialog.setMapButtonListener(mInfoButtonListener);
				mDialog.setMapIconResource(R.drawable.button_dialog_map);
				mDialog.show();
				//showPlaceInfo(getActivity(), R.layout.place_info, position);
				//Toast.makeText(getActivity(), p.getDenominazione(), Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	@Override
	public void onStart() {
		super.onStart();
		//aggiorno la view
		updateListPlaces();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.v(MainActivity.TAG, "PlacesListFragment.onDestroy()");
	}
	
	@Override
	public void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		
		Log.v(MainActivity.TAG, "PlacesListFragment.onCreate()");
	}
	
	@Override
	public void onAttach(Activity arg0) {
		super.onAttach(arg0);
		Log.v(MainActivity.TAG, "PlacesListFragment.onAttach()");
	}
	
	ControllerBinder mBinder;
	
	DialogInfoButtonListener mInfoButtonListener = new DialogInfoButtonListener() {

		@Override
		public void onButtonClicked(int placeListIndex, double latitude,
				double longitude) {
			Intent i = new Intent(MainActivity.VIEW_MAP_TAB);
			i.putExtra("latitude", latitude);
			i.putExtra("longitude", longitude);
			i.putExtra("index", placeListIndex);
			getActivity().sendBroadcast(i);
		}
	};
	
	/**
	 * Richiede una nuova lista di places, ordinata in base alla distanza dalla location corrente
	 */
	public void updateNearPlaces(){
		Log.v(MainActivity.TAG, "requesting near places");
		
		mBinder.getNearPlaces();
		
		//locadapter= new LocationAdapter(mActivity, R.layout.locationlist, list);
		//la = new ArrayAdapter<String>(mActivity, R.layout.entry ,list);
		//setListAdapter(locadapter);
	}
	
	/**
	 * Aggiorna la view, prendendo la lista disponibile
	 */
	public void updateListPlaces(){
		Log.v(MainActivity.TAG, "updating list view");
		ArrayList<Place> list = (ArrayList<Place>) Place.lista;
		locadapter= new LocationAdapter(mActivity, R.layout.locationlist, list);
		//la = new ArrayAdapter<String>(mActivity, R.layout.entry ,list);
		setListAdapter(locadapter);
	}
	
	/**
	 * called when the onServiceConnected() method in the Activity is called
	 */
	@Override
	public void onBind(ControllerBinder binder) {
		
		mBinder = binder;
		updateNearPlaces();
	}

	@Override
	public void onDataReceived() {
		updateListPlaces();
	}

	@Override
	public void onLocationReceived(int listIndex) {
		// do nothing
	}

	@Override
	public void onBookmarksListUpdated() {		
		switch(bookmarkFlag){
		case FLAG_LIST_BOOKMARKS:
			if(Place.list_bookmarks.isEmpty())
				mBinder.setBookmarks();
			//Place.temp = Place.list_bookmarks;
			Place.lista = Place.list_bookmarks;
			updateListPlaces();
			break;
		case FLAG_LIST_REFRESH:
			break;
		}
		locadapter.notifyDataSetChanged();
		//updateListPlaces();
		//getListView().invalidate(); //ridisegno la listView per colorare / scolorare di giallo il place aggiunto /tolto ai preferiti
	}
	
	
	public final static int FLAG_LIST_BOOKMARKS = 1;
	public final static int FLAG_LIST_REFRESH = 0;
	public static int bookmarkFlag = FLAG_LIST_REFRESH;
}
